import 'dart:convert';

import 'package:gold/base/config.dart';
import 'package:gold/http/apis.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/shop_model.dart';
import 'package:gold/model/user_model.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:gold/app/app.dart';

/// 描述: 缓存数据
/// 
/// @Author tangzhentao
///  @CreatedTime 19:49 2022/6/17
/// 

class CacheData {
  factory CacheData() => _instance;
  static final CacheData _instance = CacheData._internal();
  static CacheData get instance => _instance;

  static late final SharedPreferencesAsync _preferences;

  /// 用户登录凭证
  String _authorization = '';
  String get authorization => _authorization;
  set authorization(String value) {
    if (_authorization != value) {
      _authorization = value;
    }
  }

  /// 用户登录凭证
  double _collateralRate = 0.1;
  double get collateralRate => _collateralRate;

  /// 用户登录凭证
  UserModel _userInfo = UserModel();
  UserModel get userInfo => _userInfo;

  /// 用户店铺信息
  ShopModel _shopInfo = ShopModel();
  ShopModel get shopInfo => _shopInfo;

  CacheData._internal() {
    _preferences = SharedPreferencesAsync();
  }

  /// 初始化信息
  Future initInfo() async {
    _authorization = await getString(App.authorization)?? '';
    _userInfo = await _getUser();
    _shopInfo = await _getShop();
    _collateralRate = await getDouble(App.collateralRate)?? 0.1;
  }

  /// 设置登录信息
  Future setLoginSuccess(String authorization) async {
    _authorization = authorization;
    await setString(App.authorization, authorization);
  }

  /// 设置退出登录
  Future setSignOut() async {
    _authorization = '';
    await remove(App.authorization);
  }

  /// 获取用户信息
  Future<UserModel> _getUser() async {
    final data = jsonDecode(await getString(App.userInfo)?? '{}');
    return UserModel.fromJson(data);
  }

  /// 设置用户信息
  Future setUser(UserModel user) async {
    _userInfo = user;
    await setString(App.userInfo, jsonEncode(user.toJson()));
  }

  /// 获取用户店铺信息
  Future<ShopModel> _getShop() async {
    final data = jsonDecode(await getString(App.userInfo)?? '{}');
    return ShopModel.fromJson(data);
  }

  /// 设置用户店铺信息
  Future setShop(ShopModel shop) async {
    _shopInfo = shop;
    await setString(App.userInfo, jsonEncode(shop.toJson()));
  }

  /// 更新黄金回收保证金比例（%）
  Future setCollateralRate() async {
    final result = await Http().request<String>(Apis.config, data: {'key': SystemConfig.collateralRate}, showErrorMsg: false);
    if (result.success && result.data != null) {
      final rateInt = int.tryParse(result.data!)?? 10;
      final rate = rateInt / 100.0;
      _collateralRate = rate;
      await setDouble(App.collateralRate, rate);
    }
  }

  /// 保存string
  Future<void> setString(String key, String value) async {
    return _preferences.setString(key, value);
  }

  /// 保存int
  Future<void> setInt(String key, int value) async {
    return _preferences.setInt(key, value);
  }

  /// 保存double
  Future<void> setDouble(String key, double value) async {
    return _preferences.setDouble(key, value);
  }

  /// 保存bool
  Future<void> setBool(String key, bool value) async {
    return _preferences.setBool(key, value);
  }

  /// 保存List<[String]>
  Future<void> setStringList(String key, List<String> value) async {
    return _preferences.setStringList(key, value);
  }

  /// 获取String
  Future<String?> getString(String key) => _preferences.getString(key);
  /// 获取int
  Future<int?> getInt(String key) => _preferences.getInt(key);
  /// 获取double
  Future<double?> getDouble(String key) => _preferences.getDouble(key);
  /// 获取bool
  Future<bool?> getBool(String key) => _preferences.getBool(key);
  /// 获取List<[String]>
  Future<List<String>?> getStringList(String key) => _preferences.getStringList(key);

  Future<void> remove(String key) async {
    return await _preferences.remove(key);
  }

  Future<void> clear({Set<String>? allowList}) async {
    return await _preferences.clear();
  }
}